#!/usr/bin/env python
# coding=utf-8
# Author:  vavava
# Created: 04/11/2012

if __name__ == '__main__':
    """ for file debug"""
    import sys,os
    sys.path.insert(0,os.path.join( os.getcwd(), '..' ))


from vavava.basethread import BaseThread
import pickletools,pickle,socket

class BaseCommand:

    def __init__(self,name,cmd_type,data):
        self.name = name
        self.cmd_type = cmd_type
        self.data = data

    @staticmethod
    def get_msg(name,data=None):
        return pickle.dumps(BaseCommand(name,data))

    @staticmethod
    def parse(data):
        if data:
            return pickle.loads(data)

class TaskClient:

    def __init__(self,ser_host="127.0.0.1",ser_port=4444):
        self.ser_host = ser_host
        self.ser_port = ser_port

    def send_msg(self,cmd):
        sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        sock.connect((self.ser_host,self.ser_port))
        sock.send(cmd)
        #recv=sock.recv(1024)
        #print('[tcpServer said]: %s' % recv)
        sock.close()


# test code

import time

def test():
    client = TaskClient()

    client.send_msg(BaseCommand.get_msg("test","data"))

def test1():

    data = BaseCommand.get_msg("test","data")
    cmd = pickle.loads(data)
    print(cmd.name)

def main():
    try:
        while True:
            test()
            time.sleep(0.5)
    except(KeyboardInterrupt):
        print('main thread(%f):stop'%time.clock())
        print('main thread(%f):stopped'%time.clock())

if __name__ == '__main__':
    main()


